//
//  ZPMineEditIDViewController.m
//  ZPIOSEducation
//
//  Created by mini1 on 2016/12/6.
//  Copyright © 2016年 mini1. All rights reserved.
//

#import "ZPMineEditIDViewController.h"

@interface ZPMineEditIDViewController ()
@property (weak, nonatomic) IBOutlet UITextField *numId;
@property (weak, nonatomic) IBOutlet UIButton *saveBtn;
@property (weak, nonatomic) IBOutlet UILabel *headerLab;
@property (weak, nonatomic) IBOutlet UIButton *prompt;

@end

@implementation ZPMineEditIDViewController

#pragma mark - <viewDidLoad>
- (void)viewDidLoad {
    [super viewDidLoad];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
    [self.view addGestureRecognizer:tap];
    
    
}


#pragma mark - <viewWillAppear>
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.title = self.navString;
    self.headerLab.text = [NSString stringWithFormat:@"请输入%@",self.navString];
    if ([self.navString isEqualToString:@"姓名"]) {
        self.prompt.hidden = YES;
    }
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"zhenpin_back"] style:UIBarButtonItemStylePlain target:self action:@selector(clickBackButtonItem)];
}


#pragma mark - <viewDidAppear>
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange) name:UITextFieldTextDidChangeNotification object:nil];
}


#pragma mark - <textFieldDidChange>
- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
}


- (void)dismissKeyboard
{
    [self.numId resignFirstResponder];
}


- (void)clickBackButtonItem
{
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)textFieldDidChange
{
    NSString *text = self.numId.text;
    if (text.length == 0) {
        self.saveBtn.enabled = NO;
        [self.saveBtn setBackgroundColor:lightGray_color];
    } else {
        self.saveBtn.enabled = YES;
        [self.saveBtn setBackgroundColor:zFreeColor(18, 165, 232, 1)];
    }
}


- (IBAction)save:(id)sender
{
    if (self.flag == 100) {
        NSString *tempStr = self.numId.text;
        NSInteger length = self.numId.text.length;
        
        if (tempStr == nil || tempStr == NULL || [[tempStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0) {
            [ZPPromptView showMessage:@"输入姓名不能为空"];
            return;
        }
        
        if (length < 2 || length > 10) {
            [ZPPromptView showMessage:@"输入姓名限2-10个字符"];
            return;
        }
        
        self.passValue(self.numId.text);
        [self.navigationController popViewControllerAnimated:YES];
    }
    else if (self.flag == 200)
    {
        if (self.numId.text.length > 0)
        {
            if ([self.numId.text zp_isValidIdentity])
            {
                self.passValue(self.numId.text);
                [self.navigationController popViewControllerAnimated:YES];
            }
            else
            {
                [ZPPromptView showMessage:@"身份证号格式错误"];
            }
        }
        else
        {
            [ZPPromptView showMessage:@"请输入学生身份证号"];
        }
    }
}


#pragma mark - <身份证号验证>
//- (BOOL)validateIdentityID:(NSString *)userName
//{
//    //身份证号以15，18数字
//    NSString *identityRegex = @"(^[0-9]{15}$)|([0-9]{17}([0-9]|X)$)";
//    NSPredicate *identityTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",identityRegex];
//    return [identityTest evaluateWithObject:userName];
//}

@end
